package com.efei.jdbc.sql;

import org.springframework.util.StringUtils;

import java.util.List;

public class SqlUpdate extends SqlCommon{

    private final StringBuilder where = new StringBuilder();
    //private List<SqlCondition> conditions = null;
    private final StringBuilder set = new StringBuilder();
    private String table;

    public static SqlUpdate of() {
        return new SqlUpdate();
    }

    SqlUpdate() {
        super(SqlType.UPDATE);
    }

    public SqlUpdate table(String table) {
        this.table = table;
        return this;
    }

    public SqlUpdate set(String col, String alis) {
        if(StringUtils.hasLength(col)) {
            return this;
        }

        if(this.set.length()>0) {
            this.set.append(",");
        }
        set.append("set ");
        set.append(col);
        set.append("=");
        set.append(":").append(alis);
        return this;
    }

    public SqlUpdate set(String col) {
        return set(col,col);
    }

    public SqlUpdate set(SqlColumn col) {
        if(this.set.length()>0) {
            this.set.append(",");
        }

        set.append(col.getName());
        set.append("=");
        set.append(":").append(col.getAlis());

        addParameter(new SqlParameter(col.getAlis(), col.getValue(), col.getParmeterType()));
        return this;
    }

    public SqlUpdate where(String tb) {
        if(StringUtils.hasLength(tb)) {
            return this;
        }
        //this.where.append(" ");
        this.where.append(tb);
        return this;
    }

    public SqlUpdate whereAnd(String tb) {
        if(StringUtils.hasLength(tb)) {
            return this;
        }
        if(where.length()>0) {
            this.where.append(" and ");
        }
        this.where.append(tb);
        return this;
    }

    public SqlUpdate whereAnd(SqlCondition c) {
        if(where.length()>0) {
            this.where.append(" and ");
        }
        //conditions.add(c);
        this.where.append(c.getColumn());
        this.where.append(c.getOperator());
        String alis = c.getColumn().replace(".", "_");
        this.where.append(":"+alis);
        this.addParameter(alis, c.getValue());
        return this;
    }

    public SqlUpdate whereOr(String tb) {
        if(StringUtils.hasLength(tb)) {
            return this;
        }
        if(where.length()>0) {
            this.where.append("or");
        }
        this.where.append(tb);
        return this;
    }

    public SqlUpdate whereOr(SqlCondition c) {
        if(where.length()>0) {
            this.where.append("or");
        }
        //conditions.add(c);
        this.where.append(c.getColumn());
        this.where.append(c.getOperator());
        String alis = c.getColumn().replace(".", "_");
        this.where.append(":"+alis);
        this.addParameter(alis, c.getValue());
        return this;
    }

    @Override
    public StringBuilder getSql() {
        // TODO Auto-generated method stub
        StringBuilder sql = new StringBuilder();
        sql.append("update ");
        sql.append(table);
        sql.append(" set ");
        sql.append(set);
        sql.append(" where ");
        sql.append(where);
        return sql;
    }
}
